<?php
/**
 * Created by xiaosong
 * E-mail:4155433@gmail.com
 * Date: 2019/9/18
 * Time: 14:16
 */

namespace App\Models\base\traits;



trait ImgTrait
{
    public function getImgAttribute($value)
    {
        return $value ? starts_with($value, 'http') ? $value : config('filesystems.disks.admin.url').$value : '';
    }

    public function setImgAttribute($value)
    {
        $this->attributes['img'] = $value ? starts_with($value, 'http') ? $value : config('filesystems.disks.admin.url').$value : '';
    }

    public function getIconAttribute($value)
    {

        return $value ? starts_with($value, 'http') ? $value : config('filesystems.disks.admin.url').$value : '';

    }

    public function getHeaderImgAttribute($value)
    {
        return $value ? starts_with($value, 'http') ? $value : config('filesystems.disks.admin.url').$value : '';
    }


    public function setImgsAttribute($pictures)
    {
        if (is_array($pictures)) {
            $this->attributes['imgs'] = json_encode($pictures);
        }else{
            $this->attributes['imgs'] = $pictures;
        }

    }

    public function getImgsAttribute($pictures)
    {
        $imgs =  json_decode($pictures, true);

        if (!empty($imgs)){
            foreach ($imgs as $k => $value){
                $imgs[$k] = starts_with($value, 'http') ? $value : config('filesystems.disks.admin.url').$value;
            }
            return $imgs;
        }
        return [];
    }

    public function setIconsAttribute($pictures)
    {
        if (is_array($pictures)) {
            $this->attributes['icons'] = json_encode($pictures);
        }else{
            $this->attributes['icons'] = $pictures;
        }

    }

    public function getIconsAttribute($pictures)
    {
        $imgs =  json_decode($pictures, true);

        if (!empty($imgs)){
            foreach ($imgs as $k => $value){
                $imgs[$k] = starts_with($value, 'http') ? $value : config('filesystems.disks.admin.url').$value;
            }
            return $imgs;
        }
        return [];
    }

    public function setParameterAttribute($pictures)
    {
        if (is_array($pictures)) {
            $this->attributes['parameter'] = json_encode($pictures);
        }else{
            $this->attributes['parameter'] = $pictures;
        }
    }

    public function getParameterAttribute($pictures)
    {
        $imgs =  json_decode($pictures, true);

        if (!empty($imgs)){
            foreach ($imgs as $k => $value){
                $imgs[$k] = starts_with($value, 'http') ? $value : config('filesystems.disks.admin.url').$value;
            }
            return $imgs;
        }
        return [];
    }


}